package com.example.controller;
import com.example.entity.RestBean;
import com.example.service.AuthorizeService;
import com.example.util.Const;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {
    @Resource
    AuthorizeService service;
    @PostMapping("/valid-register-email")
    public RestBean<String> validateRegisterEmail(@Pattern(regexp = Const.EMAIL_REGEXP) @RequestParam("email") String email,
                                          HttpSession session) {
        String s = service.sendValidateEmail(email, session.getId(), false);
        if (s == null)
            return RestBean.success("邮件已发送，请注意查收");
        else
            return RestBean.failure(400, s);
    }
    @PostMapping("/valid-forgot-email")
    public RestBean<String> validateForgotEmail(@Pattern(regexp = Const.EMAIL_REGEXP) @RequestParam("email") String email,
                                          HttpSession session) {
        String s = service.sendValidateEmail(email, session.getId(), true);
        if (s == null)
            return RestBean.success("邮件已发送，请注意查收");
        else
            return RestBean.failure(400, s);
    }
    @PostMapping("/register")
    public RestBean<String> registerUser(@Pattern(regexp = Const.USERNAME_REGEXP) @Length(min = 2, max = 12) @RequestParam("username") String username,
                                         @Length(min = 6, max = 15) @RequestParam("password") String password,
                                         @Pattern(regexp = Const.EMAIL_REGEXP) @RequestParam("email") String email,
                                         @Length(min = 6, max = 6) @RequestParam("code") String code,
                                         HttpSession session) {
        String s = service.validateAndRegister(username, password, email, code, session.getId());
        if (s == null)
            return RestBean.success("注册成功");
        else
            return RestBean.failure(400, s);
    }
    /**
     * 1. 发送验证邮件
     * 2. 验证码是否正确，正确就在session中保存用户信息，返回token
     * 3. 用户发起重置密码请求，携带token，验证token是否正确，正确就重置密码
     */
    @PostMapping("/forgot")
    public RestBean<String> forgotPassword(@Pattern(regexp = Const.EMAIL_REGEXP) @RequestParam("email") String email,
                                           @Length(min = 6, max = 6) @RequestParam("code") String code,
                                           HttpSession session) {
        String s = service.validateAndForgot(email, code, session.getId());
        if (s == null) {
            session.setAttribute("forgot-password", email);
            return RestBean.success();
        } else {
            return RestBean.failure(400, s);
        }
    }
    @PostMapping("/do-forgot")
    public RestBean<String> doForgot(@Length(min = 6, max = 15) @RequestParam("password") String password,
                                     HttpSession session){
        String email = (String) session.getAttribute("forgot-password");
        if (email == null) {
            return RestBean.failure(401, "请先完成邮箱验证");
        } else if (service.forgotPassword(password, email)){
            session.removeAttribute("forgot-password");
            return RestBean.success("密码重置成功");
        } else {
          return RestBean.failure(500, "服务器或网络异常，请稍后再试");
        }

    }

}
